package lisp.com.event;

import org.bukkit.DyeColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Sheep;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.jetbrains.annotations.NotNull;

public class SheepEventHandler implements Listener {
    /**
     * 当玩家攻击羊羊君时，羊羊君的羊毛会随机变色
     */
    @EventHandler
    public void onPlayerHitSheep(@NotNull EntityDamageByEntityEvent event) {
        Entity attacker = event.getDamager();
        Entity damaged = event.getEntity();
        //攻击者为人并且受伤者是羊毛
        if (attacker instanceof Player && damaged instanceof Sheep sheep) {
            //获取颜色随机值
            DyeColor randomColor = DyeColor.values()[(int) (Math.random() * DyeColor.values().length)];
            //设置羊毛颜色
            sheep.setColor(randomColor);
        }
    }
}
